<?php
namespace app\shop\controller;
use app\rbac\model\AdminM;
use app\shop\model\AttributeM;
use app\shop\model\GoodCatM;
use app\shop\model\GoodsattrM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Goodsattr extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($id,'shop_goods','goods_id');
        if($res === false)
        {
            $this->error('权限不够');
        }
        $model = new GoodsattrM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $res = $model->add($data);
            if($res)
            {
                $this->success('添加成功',url('shop/Goodsattr/lst',['id'=>$data['goods_id']]));
            }
            else
            {
                $this->error($model->msg);
            }
        }

        //商品所有的属性
        $data=$model->getGoodsAttrByGoodsID($id);//商品属性
        //属性库存

        $this->assign(array(
            '_page_title'=>'添加商品属性',
            '_page_title_english'=>'Goodsattr add',
            '_btn_title'=>'商品属性列表',
            '_btn_link'=>url('lst',['id'=>$id]),
            '_page_ico'=>'iconfa-shopping-cart',
            'data'=>$data,
            'goods_id'=>$id,
        ));
        return $this->fetch();
    }



    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($id,'shop_goods','goods_id');
        if($res === false)
        {
            $this->error('权限不够');
        }
        $model = new GoodsattrM();

        //商品所有的属性
        $data=$model->getGoodsAttrByGoodsID($id);//商品属性
        //属性库存
        $gnData=Db::table('shop_goods_num')->where(['goods_id'=>$id,'is_usable'=>1])->select();

        $this->assign(array(
            '_page_title'=>'商品属性列表',
            '_page_title_english'=>'Goodsattr lst',
            '_btn_title'=>'添加商品属性',
            '_btn_link'=>url('add',['id'=>$id]),
            '_page_ico'=>'iconfa-shopping-cart',
            'data'=>$data,
            'gnData'=>$gnData,
            'goods_id'=>$id,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        $id = $this->request->post('id');
        $model = new GoodsattrM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

    /**
     * 修改库存价格
     * @param $id
     * @return mixed
     */
    public function goods_num($goods_id,$id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($goods_id,'shop_goods','goods_id');
        if($res === false)
        {
            $this->error('权限不够');
        }
        $model = new GoodsattrM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $res = $model->goods_num($data);
            if($res)
            {
                $this->success('修改成功',url('shop/Goodsattr/lst',['id'=>$data['goods_id']]));
            }
            else
            {
                $this->error($model->msg);
            }
        }

        //商品所有的属性
        $data=$model->getGoodsAttrByGoodsID($goods_id);//商品属性
        //属性库存
        $gnData=Db::table('shop_goods_num')->where(['id'=>$id,'is_usable'=>1])->find();

        $this->assign(array(
            '_page_title'=>'修改商品库存/价格',
            '_page_title_english'=>'Goodsattr edit',
            '_btn_title'=>'商品属性列表',
            '_btn_link'=>url('lst',['id'=>$goods_id]),
            '_page_ico'=>'iconfa-shopping-cart',
            'data'=>$data,
            'gnData'=>$gnData,
            'goods_id'=>$goods_id,
        ));
        return $this->fetch();
    }


}
